﻿using _2112A_2_MES._chubowen;
using _2112A_2_MES.WangWeiHaos.Inputs;
using _2112A_2_MES.WangWeiHaos.IService;
using _2112A_2_MES.WangWeiHaos.Outputs;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Test.WangWeiHao;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _2112A_2_MES.WangWeiHao.Service
{
    [ApiExplorerSettings(GroupName = "王炜豪")]
    public class ProductClassificationService : ApplicationService, IProductClassificationService
    {
        public readonly IRepository<MdItemType, int> _mdItemTypeRepository;

        public ProductClassificationService(IRepository<MdItemType, int> mdItemTypeRepository)
        {
            _mdItemTypeRepository = mdItemTypeRepository;
        }

        /// <summary>
        /// 添加产品物料分类
        /// </summary>
        /// <param name="mdItem">实体</param>
        /// <returns></returns>
        [HttpGet, Route("/api/ProductClassification/AddProductClassification")]
        public async Task<int> AddProductClassification(MdItemType mdItem)
        {
            mdItem.ItemTypeCode = "12345678";
            mdItem.CreateBy = "wangweihao";
            mdItem.CreateTime = DateTime.Now;
            await _mdItemTypeRepository.InsertAsync(mdItem);
            return await Task.FromResult(1);
        }

        /// <summary>
        /// 删除产品物料分类
        /// </summary>
        /// <param name="id">产品物料分类编号</param>
        /// <returns></returns>
        [HttpGet, Route("/api/ProductClassification/DeleteProductClassification")]
        public async Task<int> DeleteProductClassification(int id)
        {
            await _mdItemTypeRepository.DeleteAsync(id);
            return await Task.FromResult(1);
        }

        /// <summary>
        /// 获取产品物料分类
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        [HttpGet, Route("/api/ProductClassification/GetMdItemType")]
        public async Task<MdItemType> GetMdItemType(int id)
        {
            var mdItem = await _mdItemTypeRepository.GetAsync(id);
            return mdItem;
        }

        /// <summary>
        /// 显示产品物料分类
        /// </summary>
        /// <returns></returns>
        [Route("/api/ProductClassification/GetProcessPriorityClassOutputs")]
        public async Task<ReturnSelect<object>> GetProcessPriorityClassOutputs(string? itemTypeName, string? enableFlag, int pageIndex, int pageSize)
        {
            var list = await _mdItemTypeRepository.GetListAsync();
            if (itemTypeName!= null)
            {
                list = list.Where(x => x.ItemTypeName.Contains(itemTypeName)).ToList();
            }
            if (enableFlag != null)
            {
                list = list.Where(x => x.EnableFlag.Contains(enableFlag)).ToList();
            }
            var totalCount = list.Count;
            var pageList = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var productClassificationList = AutoMapperExt.MapToList<MdItemType, ProductClassificationOutputs>(pageList);
            var data = new { TotalCount = totalCount, PageList = productClassificationList };
            if (productClassificationList != null)
            {
                return new ReturnSelect<object>(true, "0", data);
            }
            else
            {
                return new ReturnSelect<object>(true, "0", null);
            }

        }

        /// <summary>
        /// 修改产品物料分类
        /// </summary>
        /// <param name="mdItem">实体</param>
        /// <returns></returns>
        [HttpGet, Route("/api/ProductClassification/UpdateProductClassification")]
        public async Task<int> UpdateProductClassification(ProductClassificationInput productClassificationInput)
        {
            var mdItem = await _mdItemTypeRepository.GetAsync(productClassificationInput.id);
            mdItem.ItemTypeName = productClassificationInput.ItemTypeName;
            mdItem.OrderNum = productClassificationInput.OrderNum;
            mdItem.ItemOrProductId = productClassificationInput.ItemOrProductId;
            mdItem.EnableFlag = productClassificationInput.EnableFlag;
            mdItem.UpdateBy = "wangweihao";
            mdItem.UpdateTime = DateTime.Now;
            await _mdItemTypeRepository.UpdateAsync(mdItem);
            return await Task.FromResult(1);
        }
    }
}
